/*
*
* Created by yuri at Apr 2, 2011
*
*/



package implementation.server;

import implementation.DB.CDataBase;
import implementation.shared.EPacketParam;
import implementation.shared.EPacketType;
import implementation.shared.IPacket;
import implementation.shared.SharedFactory;
import implementation.shared.stubs.*;

public class ServerFactory {
	
	private static IDB _db;
	private static IServerConnectionManager _serverConnectionManager;
	private static ISessionManager _sessionManager;
	
	public static ISession createSession(IConnection connection, IServer system) {
		return new Session(connection, system);
	}
	
	public static IDB getBDSingletone() {
		if (_db == null) {
			//_db = new CDB();
			_db=new CDataBase();
		}
		return _db;
	}
	
	public static IServerConnectionManager getServerConnectionManagerSingletone(ISessionManager sessionManager) {
		if (_serverConnectionManager == null) {
			_serverConnectionManager = new ServerConnectionManager(sessionManager);
		} 
		return _serverConnectionManager; 
	}
	
	public static ISessionManager getSessionManagerSingletone(IServer system) {
		if (_sessionManager == null) {
			_sessionManager = new SessionManager(system);
		}
		return _sessionManager;
	}
	
	public static IPacket createSuccessPacket(String msg) {
		IPacket packet = SharedFactory.createPacket(EPacketType.ESuccess);
		packet.addParam(EPacketParam.EMessage, msg);
		return packet;
	}
	
	public static IPacket createFailPacket(String errMsg) {
		IPacket packet = SharedFactory.createPacket(EPacketType.EFailure);
		packet.addParam(EPacketParam.EMessage, errMsg);
		return packet;
	}
	
	public static IForumStub createForumStub(String name, int id, int threadCount) {
		return new CForumStub(name,id,threadCount);
	}
	
	public static IThreadStub createThreadStub(int threadId,String topic,int authorID,int postCount) {
		return new CThreadStub(threadId,topic,authorID,postCount);
	}
	
	public static IPostStub createPostStub(int postID,String topic,String content,int authorID) {
		return new CPostStub(postID,topic,content,authorID);
	}

	public static IUserStub createUserStub(String username, int id) {
		return new CUserStub(username, id);
	}
	
}
